(function(){
    _$(document).ready(function(){
        function App(){};
        function excludeSpecial(s) {
            // 去掉转义字符
            s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
            // 去掉特殊字符
            s = s.replace(/[\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/);
            return s;
        };
        var page=0;
        var key=true;
        App.prototype.ajaxFn=function(){
            _$.post('/getForumInfo',{bid:bid,brid:getQueryString('brid')},function(data){
                console.log(data);
                if(data['code']==1){
                    var brinfo=data['info']['brinfo'];
                    var urinfo=data['info']['urinfo'];
                    _$("#bName").html(urinfo.nickname);
                    _$("#times").html(getTimes(brinfo.startTime));
                    _$("#title").html(brinfo.title);
                    _$("#cnt").html(brinfo.cnt);
                    _$("#avatar").attr('src',urinfo.imgurl).attr('alt',urinfo.nickname+"的头像");
                }
            });
            _$.post('/getForumUserCount',{bid:bid,brid:getQueryString('brid')},function(data){
                console.log(data);
                if(data['code']==1){
                    _$("#replyCount").html(data.count);
                }
            });
            //全部评论
            _$.ajax({
                type: 'post',
                url: '/getForumUserCount',
                data: {bid:bid,brid:getQueryString('brid')},
                async: false,
                success: function (json) {
                    var dropload = $('#postListContainer').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已显示全部数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me) {
                            var pageLength = json['count'];
                            console.log(pageLength);
                            if (json['count'] % 10 > 0) {
                                pageLength = (json['count'] / 10) + 1;
                            }
                            if (page > pageLength) {
                                me.lock();
                                me.noData();
                                me.resetload();
                            } else {
                                console.log(page);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getForumUserList',
                                    data: {page: page, bid: bid,brid:getQueryString('brid'),key:key},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            key=false;
                                            var info = data.info.data;
                                            var brimg=data.info.userInfo.userimg;
                                            var brnick=data.info.userInfo.usernick;
                                            if (info == "") {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            } else {
                                                for(var i=0;i<info.length;i++){
                                                    info[i].imgurl=brimg[i];
                                                    info[i].nickname=brnick[i];
                                                }
                                                if (info.length > 0) {
                                                    var html="";
                                                    for(var val in info){
                                                        info[val]['startTime']=getTimes(info[val]['startTime']);
                                                        html += template('reviewtpl', info[val]);
                                                    }
                                                    // 为了测试，延迟1秒加载
                                                    setTimeout(function () {
                                                        // 插入加载使用 html() 重置 DOM
                                                        _$("#postListContainer .dropload-down").before(html);
                                                        page++;
                                                        // 每次数据加载完，必须重置
                                                        me.resetload();
                                                    }, 1000);
                                                }
                                            }
                                        }else if(data.code==2){
                                            _$("#postListContainer").html('<div class="null-x"><div class="null">快来抢沙发吧~</div></div>');
                                            // 每次数据加载完，必须重置
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }else {
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
        };
        App.prototype.eventFn=function(){
            _$("#commReplyPopup").attr('data-brid',getQueryString('brid'));
            _$("#huifu").click(function(){
                _$("#halert").fadeIn();
            })
            _$("#closealert,#commReplyCancel").click(function(){
                _$("#halert").fadeOut();
            });
            _$("#fabiao").click(function(){
                var cnt=_$("#post-content-input").val();
                if(excludeSpecial(cnt).length<1){
                    $("#post-content-input").focus();
                }else if(excludeSpecial(cnt).length>2000){
                    tip("内容大于2000个字");
                }else{
                    _$.ajax({
                        type: 'post',
                        url: '/addForumUserCnt',
                        data: {bid: bid,brid:getQueryString('brid'),cnt:cnt},
                        async: false,
                        success: function (data) {
                            console.log(data);
                            if(data.code==1){
                                tip("发布成功");
                                setTimeout(function(){
                                    location.href='/book/thread/'+bid+'?brid='+getQueryString('brid');
                                },1000);
                            }
                        }
                    });
                }
            });
        };
        App.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();

        };
        //程序初始化
        new App().init();
    });
})();